var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxOrgRModel = function (obj) {
    YjxBaseModel.call(this,obj);

    // `org_id` bigint(20) unsigned NOT NULL COMMENT '公司组织id',
    // `user_id` bigint(20) NOT NULL COMMENT '用户id',
    // `role` bigint(20) NOT NULL DEFAULT '0' COMMENT '用户角色(多角色用户按位与): \\\\n0普通用户, 1一级管理员, 2二级管理员, 4三级管理员, 8超级管理员, 16(待定), 32(待定), 64(待定)',
    // `state` int(2) NOT NULL DEFAULT '10' COMMENT '组织关系状态: 10有效, 20无效',
    // `desc` varchar(500) DEFAULT NULL COMMENT '备注',
    // `create_time` datetime NOT NULL,
    // `update_time` datetime NOT NULL,
    // `update_by` varchar(50) NOT NULL DEFAULT '',
    // `del_flg` int(1) NOT NULL DEFAULT '0'

};

YjxOrgRModel.prototype = new YjxBaseModel();

// 取得表名
YjxOrgRModel.prototype.getTableName = function(){
    return 'yjx_org_r';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxOrgRModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'user_id='+this.user_id+' AND org_id='+this.org_id;
};

/**
 * 获取用户的组织信息
 *
 * @author 王俊
 * @date 2018-06-11
 *
 * @param userId    用户唯一标识
 * @param callback  回调
 *          error:错误
 *          data : [
 *              {
 *                  id      组织 id
 *                  name    组织名称
 *                  type    组织类型
 *                  role    用户角色
 *              }
 *          ]
 */
YjxOrgRModel.prototype.getUserOrganizationsInfo = function (userId, callback) {

    pool.pool.getConnection(function(getConErr, connection) {
        if (getConErr) {
            console.log('YjxOrgRModel.getUserOrganizationsInfo Err:'+err);
            return callback(getConErr,null);
        }
        var sql =
            ' SELECT yo.org_id AS id, ' +
            '        yo.name, ' +
            '        yo.type, ' +
            '        yor.role ' +
            ' FROM yjx.yjx_org yo, ' +
            '      yjx.yjx_org_r yor ' +
            ' WHERE yo.del_flg = 0 ' +
            '   AND yor.del_flg = 0 ' +
            '   AND yo.state = 4 ' +
            '   AND yor.user_id = ' + userId +
            '   AND yor.org_id = yo.org_id ';
        console.log(sql);
        connection.query(sql, function(queryErr, results) {
            // And done with the connection.
            connection.release();
            return callback(queryErr, results);
        });
    });
};

/**
 * 获取用户所在的网红公司的组织信息
 *
 * @author
 * @date 2018-06-12
 *
 * @param userId    用户唯一标识
 * @param callback  回调
 *          error:错误
 *          data : [
 *              {
 *                  id      组织 id
 *                  name    组织名称
 *                  type    组织类型
 *                  role    用户角色
 *              }
 *          ]
 */
YjxOrgRModel.prototype.getUserPartnerOrgInfo = function (userId, callback) {

    pool.pool.getConnection(function(getConErr, connection) {
        if (getConErr) {
            console.log('YjxOrgRModel.getUserPartnerOrgInfo Err:'+err);
            return callback(getConErr,null);
        }
        var sql =
            ' SELECT yo.org_id AS id, ' +
            '        yo.name, ' +
            '        yo.type, ' +
            '        yor.role ' +
            ' FROM yjx.yjx_org yo, ' +
            '      yjx.yjx_org_r yor ' +
            ' WHERE yo.del_flg = 0 ' +
            '   AND yor.del_flg = 0 ' +
            '   AND yo.state = 4 ' +
            '   AND yo.type = 400 ' +
            '   AND yor.user_id = ' + userId +
            '   AND yor.org_id = yo.org_id ';
        console.log(sql);
        connection.query(sql, function(queryErr, results) {
            // And done with the connection.
            connection.release();
            return callback(queryErr, results);
        });
    });
};




module.exports = YjxOrgRModel;

